import wx from '@/assets/jsSDK1.6.0.js'
import Api from '@/request/data.js'
import constValues from '@/request/constValues.js'
import SnowflakeId from "snowflake-id"

export default {
  snowflakeId() {
    const id = new SnowflakeId()
    return id.generate()
  },
  wxshareSign(obj) {
    //
		// obj.snowId = this.snowflakeId()		
		obj.shareUrl = `${obj.shareUrl}&new_id=${obj.snowId}`				
    let urlStr = location.href.split('#')[0]
    let params = {
      url: urlStr
    }
    Api.get(constValues.wechatSign, params).then(res => {
      if (res.code == 200) {
        let dataDic = res.data
        wx.config({
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId: dataDic.appId, // 必填，公众号的唯一标识
          timestamp: dataDic.timestamp, // 必填，生成签名的时间戳
          nonceStr: dataDic.nonceStr, // 必填，生成签名的随机串
          signature: dataDic.signature, // 必填，签名
          jsApiList: ["updateAppMessageShareData", "updateTimelineShareData", "onMenuShareAppMessage",
            "onMenuShareTimeline"
          ], // 必填，需要使用的JS接口列表
        });
        this.shareConfig(dataDic, obj)
      } else {
        uni.showToast({
          icon: 'none',
          title: res.msg
        })
      }
    })
  },


  shareConfig(obj, obj2) {
    let _this = this
    wx.ready(function() {									
      let shareData = {
        title: obj2.shareTitle,
        desc: obj2.shareDes,
        link: obj2.shareUrl,
        imgUrl: obj2.shareImgUrl
      }
      // console.error('shareData====>',shareData)
      // wx.updateAppMessageShareData(shareData); //分享给好友
      // wx.updateTimelineShareData(shareData); //分享到朋友圈
      // wx.onMenuShareAppMessage(shareData) //废弃了分享给朋友
      // wx.onMenuShareTimeline(shareData)		//废弃了分享到朋友圈			

      wx.onMenuShareAppMessage({
        title: obj2.shareTitle,
        desc: obj2.shareDes,
        link: obj2.shareUrl,
        imgUrl: obj2.shareImgUrl,
        trigger: () => {
          console.log('点击分享onMenuShareAppMessage')
          _this.shareHeadline(obj2, {
            type: 1
          })
        },
        success: () => {
          console.log('分享成功onMenuShareAppMessage')
        },
        cancel: () => {
          console.log('分享取消onMenuShareAppMessage')
        },
        fail: () => {
          console.log('分享失败onMenuShareAppMessage')
        }
      })

      wx.onMenuShareTimeline({
        title: obj2.shareTitle,
        desc: obj2.shareDes,
        link: obj2.shareUrl,
        imgUrl: obj2.shareImgUrl,
        trigger: () => {
          console.log('点击分享onMenuShareTimeline')
          _this.shareHeadline(obj2, {
            type: 2
          })
        },
        success: () => {
          console.log('分享成功onMenuShareTimeline')
        },
        cancel: () => {
          console.log('分享取消onMenuShareTimeline')
        },
        fail: () => {
          console.log('分享失败onMenuShareTimeline')
        }
      })


    });

    wx.error(function(res) {
      console.log('========wxshare error========')
      console.log(res)
    });

  },

  shareHeadline(shareObj, obj) {
    let type = ''
    if (obj.type == 1) {
      type = 'SHARE_CHAT'
    } else {
      type = 'SHARE_MOMENTS'
    }

    let params1 = {}
    if (shareObj.id) {
      params1 = {
        id: shareObj.id,
        manager_id: shareObj.manager_id,
        receiver_id: uni.getStorageSync('zhihuanId'), //USER对应 客户经理id  WECHAT_INFO对应微信openid
        receiver_realm: 'WECHAT_INFO', //访问者类别：USER-本地用户；WECHAT_INFO-微信用户,
        type: type, //SHARE_CHAT-分享到好友聊天；SHARE_MOMENTS-分享到朋友圈
        title: shareObj.shareTitle,
        parent_id: shareObj.share_id,
        share_id: shareObj.snowId,
        respondent_realm: shareObj
          .respondent_realm, //分享类型：ARTICLE-资讯；HEADLINE-早报；LIVE-直播；CARD-名片 POSTER-精品海报  OWNER_CONTENT-转发助手；LEARN-精品课程
        self_share: '2' //业务员本人分享 1是 2否
      }
    } else {
      params1 = {
        manager_id: shareObj.manager_id,
        receiver_id: uni.getStorageSync('zhihuanId'), //USER对应 客户经理id  WECHAT_INFO对应微信openid
        receiver_realm: 'WECHAT_INFO', //访问者类别：USER-本地用户；WECHAT_INFO-微信用户,
        type: type, //SHARE_CHAT-分享到好友聊天；SHARE_MOMENTS-分享到朋友圈
        title: shareObj.shareTitle,
        parent_id: shareObj.share_id,
        share_id: shareObj.snowId,
        respondent_realm: shareObj
          .respondent_realm, //分享类型：ARTICLE-资讯；HEADLINE-早报；LIVE-直播；CARD-名片 POSTER-精品海报  OWNER_CONTENT-转发助手；LEARN-精品课程
        self_share: '2' //业务员本人分享 1是 2否
      }
    }


    Api.post(constValues.shareHeadline, params1).then(res => {
      console.log(res)
      // uni.showModal({
      //   title: JSON.stringify(res)
      // })
      if (res.code == 200) {

      }
    })
  }
}